﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VkSample82
{
    public class BallData
    {
        public float[] Data { get; set; }

        public uint VCount { get; set; }

        public float ToRadians(float degree) => MathF.PI * degree / 180.0f;

        public float[] GenerateTexCoor(int bw, int bh)
        {
            var result = new float[bw * bh * 6 * 2];
            float sizew = 1.0f / bw;
            float sizeh = 1.0f / bh;
            int c = 0;
            for(int i = 0; i < bh; i++)
            {
                for(int j = 0; j < bw; j++)
                {
                    float s = j * sizew;
                    float t = i * sizeh;
                    result[c++] = s;
                    result[c++] = t;
                    result[c++] = s;
                    result[c++] = t + sizeh;
                    result[c++] = s + sizew;
                    result[c++] = t;
                    result[c++] = s + sizew;
                    result[c++] = t;
                    result[c++] = s;
                    result[c++] = t + sizeh;
                    result[c++] = s + sizew;
                    result[c++] = t + sizeh;
                }
            }
            return result;
        }

        public void GenBallData(float angleSpan)
        {
            const float r = 30.0f;
            List<float> alVertix = new List<float>();
            for(float vAngle = 90; vAngle > -90; vAngle -= angleSpan)
            {
                for(float hAngle = 360; hAngle > 0; hAngle -= angleSpan)
                {
                    double xozLength = r * MathF.Cos(ToRadians(vAngle));
                    float x1 = (float)(xozLength * MathF.Cos(ToRadians(hAngle)));
                    float z1 = (float)(xozLength * MathF.Sin(ToRadians(hAngle)));
                    float y1 = (float)(r * MathF.Sin(ToRadians(vAngle)));
                    xozLength = r * MathF.Cos(ToRadians(vAngle - angleSpan));
                    float x2 = (float)(xozLength * MathF.Cos(ToRadians(hAngle)));
                    float z2 = (float)(xozLength * MathF.Sin(ToRadians(hAngle)));
                    float y2 = (float)(r * MathF.Sin(ToRadians(vAngle - angleSpan)));
                    xozLength = r * MathF.Cos(ToRadians(vAngle - angleSpan));
                    float x3 = (float)(xozLength * MathF.Cos(ToRadians(hAngle - angleSpan)));
                    float z3 = (float)(xozLength * MathF.Sin(ToRadians(hAngle - angleSpan)));
                    float y3 = (float)(r * MathF.Sin(ToRadians(vAngle - angleSpan)));
                    xozLength = r * MathF.Cos(ToRadians(vAngle));
                    float x4 = (float)(xozLength * MathF.Cos(ToRadians(hAngle - angleSpan)));
                    float z4 = (float)(xozLength * MathF.Sin(ToRadians(hAngle - angleSpan)));
                    float y4 = (float)(r * MathF.Sin(ToRadians(vAngle)));
                    alVertix.Add(x1); alVertix.Add(y1); alVertix.Add(z1);
                    alVertix.Add(x2); alVertix.Add(y2); alVertix.Add(z2);
                    alVertix.Add(x4); alVertix.Add(y4); alVertix.Add(z4);
                    alVertix.Add(x4); alVertix.Add(y4); alVertix.Add(z4);
                    alVertix.Add(x2); alVertix.Add(y2); alVertix.Add(z2);
                    alVertix.Add(x3); alVertix.Add(y3); alVertix.Add(z3);
                }
            }
            float[] texCoor = GenerateTexCoor((int)(360 / angleSpan), (int)(180 / angleSpan));
            VCount = (uint)alVertix.Count / 3;
            Data = new float[VCount * 8];
            int index = 0;
            int texIndex = 0;
            for(int i = 0; i < VCount; i++)
            {
                Data[index++] = alVertix[i * 3 + 0];
                Data[index++] = alVertix[i * 3 + 1];
                Data[index++] = alVertix[i * 3 + 2];
                Data[index++] = texCoor[texIndex++];
                Data[index++] = texCoor[texIndex++];
                Data[index++] = alVertix[i * 3 + 0] / r;
                Data[index++] = alVertix[i * 3 + 1] / r;
                Data[index++] = alVertix[i * 3 + 2] / r;
            }
        }
    }
}
